package dz.chuxin.app.core;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;

import com.trello.rxlifecycle.components.support.RxAppCompatActivity;

import dz.chuxin.app.R;
import dz.chuxin.app.sample.HomeActivity;

/**
 * Activity基类
 * Toolbar 独立使用
 * Created by zhyh on 2016/6/29.
 */
public abstract class BaseActivity extends RxAppCompatActivity implements Toolbar.OnMenuItemClickListener {

    protected Toolbar mToolbar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        switch (AppStatusTracker.getInstance().getAppStatus()) {
            case ConstantValues.APP_STATUS_FORCE_KILLED:
                protectApp();
                break;
            case ConstantValues.APP_STATUS_KICK_OUT:
                // TODO: 2016/6/30 一般是不同设备登录同一个帐号 ，类似 QQ 只允许一个帐号登录
                break;
            case ConstantValues.APP_STATUS_OFFLINE:
            case ConstantValues.APP_STATUS_LOGOUT:
            case ConstantValues.APP_STATUS_ONLINE:
                setUpContentView();
                setUpData(savedInstanceState);
                break;
        }
    }

    protected void setUpData(Bundle savedInstanceState) {
    }

    // 设置Activity的页面布局
    protected abstract void setUpContentView();

    protected void protectApp() {
        Intent intent = new Intent(this, HomeActivity.class);
        intent.putExtra(ConstantValues.KEY_HOME_ACTION, ConstantValues.ACTION_RESTART_APP);
        startActivity(intent);
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        setContentView(layoutResID, -1);
    }

    public void setContentView(@LayoutRes int layoutResID, int titleResId, int menuResId, int activityMode) {
        super.setContentView(layoutResID);

        if (activityMode == ConstantValues.ACTIVITY_MODE_NONE) return;

        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        if (mToolbar == null) return;

        if (activityMode == ConstantValues.ACTIVITY_MODE_BACK)
            mToolbar.setNavigationIcon(R.mipmap.ic_arrow_back_white_36dp);

        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onNavigationBtnClicked();
            }
        });

        if (titleResId > 0) mToolbar.setTitle(titleResId);
        setUpMenu(menuResId);
        setUpToolbarComplete(mToolbar);
    }

    /**
     * Toolbar 配置结束回调方法
     * @param toolbar 当前界面的 Toolbar 实例
     */
    protected void setUpToolbarComplete(Toolbar toolbar) {
    }

    public void setUpTitle(int titleResId) {
        mToolbar.setTitle(titleResId);
    }

    public void setUpTitle(String title) {
        mToolbar.setTitle(title);
    }

    // 配置Toolbar菜单项
    protected void setUpMenu(int menuResId) {
        if (mToolbar == null) return;
        mToolbar.getMenu().clear();
        if (menuResId < 0) return;
        mToolbar.inflateMenu(menuResId);
        mToolbar.setOnMenuItemClickListener(this);
    }

    public void setContentView(@LayoutRes int layoutResID, int titleResId) {
        setContentView(layoutResID, titleResId, -1, ConstantValues.ACTIVITY_MODE_BACK);
    }

    public void setContentView(@LayoutRes int layoutResID, int titleResId, int menuResId) {
        setContentView(layoutResID, titleResId, menuResId, ConstantValues.ACTIVITY_MODE_BACK);
    }

    // Toolbar导航按钮被点击响应，默认为退出Activity
    protected void onNavigationBtnClicked() {
        finish();
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        return false;
    }
}
